package com.cctc.demo.controller;

import java.util.List;
import java.util.concurrent.Callable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cctc.demo.domain.Product;
import com.cctc.demo.services.ProductService;
import com.cctc.framework.domain.EntityForGrid;

@Controller
@RequestMapping(value="/demo")
public class ProductController
{
    @Autowired
    private ProductService<Product> productService;
    
    @ResponseBody
    @RequestMapping(value="/product.json")
    public Callable<EntityForGrid> getAllProduct(@RequestParam("rows") final  int pageSize, @RequestParam("page") final int pageNumber)
    {
        return new Callable<EntityForGrid>() {
            int temppageSize=0;
            @Override
            public EntityForGrid call() throws Exception
            {
                if (pageSize * pageNumber > getALLProductSize())
                {
                    temppageSize = getALLProductSize() - (pageNumber - 1) * pageSize;
                }
                EntityForGrid efg = new EntityForGrid(getALLProductSize(), getProduct(pageNumber - 1, temppageSize));
                return efg;
            }
        };
        //int pageSize = Integer.valueOf(request.getParameter("rows"));
       // int pageNumber = Integer.valueOf(request.getParameter("page"));
        
    }
    
    private List<Product> getProduct(int pageNumber, int pageSize)
    {
        return (List<Product>) productService.getAllProduct(pageNumber, pageSize);
    }
    
    private int getALLProductSize()
    {
        return productService.getAllProductCount();
    }
}
